package com.carltechnology.mahjonggamecaculation.mode.impl;

import android.content.ContentValues;
import android.database.Cursor;
import android.os.Parcel;
import android.os.Parcelable;

import com.carltechnology.mahjonggamecaculation.mode.IGameData;
import com.carltechnology.mahjonggamecaculation.utils.Utils;
import com.github.stuxuhai.jpinyin.PinyinHelper;

import java.util.Arrays;

/**
 * Created by WZTCM on 2016/6/27.
 */
public class GameDataMode implements IGameData {
    private enum DATA_COLUMN_NAME {
        COLUMN_ID("id"),
        COLUMN_NAME("name"),
        COLUMN_DATE("date");

        private String name;

        DATA_COLUMN_NAME(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return name;
        }
    }

    private int id;
    private String date;
    private String[] names;



    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(date);
        dest.writeString(getValue());
    }

    /**
     * the class here used for generating the information fingerprint.
     * here we use an opensource framework jpinyin to transfer
     * chinese character into pingying which only has the first character.
     */
    private static class FingerPrinterHelper {
        /**
         * get the generated id.
         * @param values the chinese character
         * @return the information fingerprint id.
         */
        public int generateID(String[] values) {
            int id = 0;
            for (String tmp : values) {
                String pinYin = PinyinHelper.getShortPinyin(tmp);
                id += getNameFingerPrint(pinYin);
            }
            return id;
        }
        // get a fingerprint
        private int getNameFingerPrint(String pinYin) {
            int tmp = 0;
            int len = pinYin.length();
            for (int i = 0; i < len; i++) {
                tmp = tmp << 5;
                tmp += (pinYin.charAt(i) - 'a');
            }
            return tmp;
        }
    }

    private GameDataMode(int id, String[] names) {
        this.id = id;
        this.names = names;
        date = Utils.getInstance().getTodayDate();
    }

    private GameDataMode(int id, String[] names, String date) {
        this.id = id;
        this.names = names;
        this.date = date;
    }

    private GameDataMode(Parcel in){
        id = in.readInt();
        date = in.readString();
        names = in.readString().split("_");
    }

    @Override
    public int getId() {
        return id;
    }

    @Override
    public String[] getValues() {
        return names;
    }

    @Override
    public String getDate() {
        return date;
    }

    @Override
    public String getValue() {
        StringBuilder sb = new StringBuilder();
        sb.append(names[0]).append("_")
                .append(names[1]).append("_")
                .append(names[2]).append("_")
                .append(names[3]);

        return sb.toString();
    }

    @Override
    public void update() {
        this.date = Utils.getInstance().getTodayDate();
    }

    public static final Parcelable.Creator<GameDataMode> CREATOR
            = new Creator<GameDataMode>() {
        @Override
        public GameDataMode createFromParcel(Parcel source) {
            return new GameDataMode(source);
        }

        @Override
        public GameDataMode[] newArray(int size) {
            return new GameDataMode[size];
        }
    };

    @Override
    public ContentValues getContentValues() {
        ContentValues contentValues = new ContentValues();
        contentValues.put(String.valueOf(DATA_COLUMN_NAME.COLUMN_ID), getId());
        contentValues.put(String.valueOf(DATA_COLUMN_NAME.COLUMN_NAME), getValue());
        contentValues.put(String.valueOf(DATA_COLUMN_NAME.COLUMN_DATE), getDate());
        return contentValues;
    }


    /**
     * transfer cursor into GameDataMode
     * @param cursor the result from an querying
     * @return a new GameDataMode.
     */
    public static GameDataMode transferCursor(Cursor cursor) {
        int id = cursor.getInt(cursor.getColumnIndex(String.valueOf(DATA_COLUMN_NAME.COLUMN_ID)));
        String[] names = cursor.getString(cursor.getColumnIndex(String.valueOf(DATA_COLUMN_NAME.COLUMN_NAME))).split("_");
        String date = cursor.getString(cursor.getColumnIndex(String.valueOf(DATA_COLUMN_NAME.COLUMN_DATE)));
        return new GameDataMode(id, names, date);
    }

    @Override
    public String toString() {
        return "GameDataMode{" +
                "id=" + id +
                ", date='" + date + '\'' +
                ", names=" + Arrays.toString(names) +
                '}';
    }

    public static GameDataMode _of(String[] names) {
        FingerPrinterHelper helper = new FingerPrinterHelper();
        return new GameDataMode(helper.generateID(names), names);
    }

    public static GameDataMode _of(String[] names, String date) {
        FingerPrinterHelper helper = new FingerPrinterHelper();
        return new GameDataMode(helper.generateID(names), names, date);
    }
}
